package com.example.excel.ui.activity;

import android.app.Application;

import com.example.excel.base.BaseViewModel;
import com.example.excel.base.CommonCallback;
import com.example.excel.bean.InventoryBean;
import com.example.excel.config.Constants;
import com.example.excel.db.InventoryController;
import com.example.excel.utils.POIExcelUtils;
import com.example.excel.utils.SharedPreferencesUtils;
import com.example.excel.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class MainViewModel2 extends BaseViewModel {
    private static final String TAG = MainViewModel2.class.getSimpleName();

    public MutableLiveData<String> mShowUI = new MutableLiveData<>();
    public MutableLiveData<Boolean> mLoadSuccess = new MutableLiveData<>();
    public MutableLiveData<Boolean> mUploadSuccess = new MutableLiveData<>();
    public int mTotal = -1;

    public MainViewModel2(@NonNull Application application) {
        super(application);
    }

    // 资料下载
    public void loadInventoryData(final String filePath) {
        getDialog().setValue(true);
        mTotal = -1;
        Observable.just(1).map(new Function<Integer, Integer>() {
            @io.reactivex.annotations.NonNull
            @Override
            public Integer apply(@io.reactivex.annotations.NonNull Integer integer) throws Exception {
                InventoryController.getInstance().deleteAll();
                SharedPreferencesUtils.setString(Constants.EXCEL_FILE_PATH, filePath);
                return POIExcelUtils.importExcel(filePath, new CommonCallback<List<String>>() {
                    @Override
                    public void onResult(List<String>... data) {
                        List<String> list = data[0];
                        SharedPreferencesUtils.setString(Constants.EXCEL_TITLE, new Gson().toJson(list));
                    }
                });
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

            }

            @Override
            public void onNext(@io.reactivex.annotations.NonNull Integer integer) {
                mTotal = integer;
                if (integer != -1) {
                    mLoadSuccess.setValue(true);
                } else {
                    mLoadSuccess.setValue(false);
                }
            }

            @Override
            public void onError(@io.reactivex.annotations.NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                getDialog().setValue(false);
            }
        });
    }

    public void uploadInventoryData(final String filePath) {
        getDialog().setValue(true);
        mTotal = -1;
        Observable.just(1).map(new Function<Integer, Boolean>() {
            @io.reactivex.annotations.NonNull
            @Override
            public Boolean apply(@io.reactivex.annotations.NonNull Integer integer) throws Exception {
                String titleJson = SharedPreferencesUtils.getString(Constants.EXCEL_TITLE, "");
                List<String> list = new Gson().fromJson(titleJson, new TypeToken<List<String>>() {
                }.getType());
                String[] titles = list.toArray(new String[]{});
                List<InventoryBean> inventoryBeans = InventoryController.getInstance().queryAllInventory();
                return POIExcelUtils.writeObjListToExcel(inventoryBeans, filePath,titles);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Boolean>() {
            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

            }

            @Override
            public void onNext(@io.reactivex.annotations.NonNull Boolean integer) {
                if (integer) {
                    mUploadSuccess.setValue(true);
                } else {
                    mUploadSuccess.setValue(false);
                }
            }

            @Override
            public void onError(@io.reactivex.annotations.NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                getDialog().setValue(false);
            }
        });
    }

    public void clear() {
        Observable.just(1).map(new Function<Integer, Boolean>() {
            @io.reactivex.annotations.NonNull
            @Override
            public Boolean apply(@io.reactivex.annotations.NonNull Integer integer) throws Exception {
                return InventoryController.getInstance().deleteAll();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Boolean>() {
            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

            }

            @Override
            public void onNext(@io.reactivex.annotations.NonNull Boolean aBoolean) {
                if (aBoolean) {
                    SharedPreferencesUtils.setBoolean(Constants.LOAD_COMPLE, false);
                    SharedPreferencesUtils.setBoolean(Constants.UPLOAD_COMPLE, false);
                    SharedPreferencesUtils.setString(Constants.EXCEL_FILE_PATH, "");
                    SharedPreferencesUtils.setString(Constants.EXCEL_TITLE, "");
                    mShowUI.setValue("清除本地数据成功");
                } else {
                    mShowUI.setValue("清除本地数据失败，请重新清理");
                }
            }

            @Override
            public void onError(@io.reactivex.annotations.NonNull Throwable e) {
                Utils.getInstance().showToast("e:" + e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

}
